package com.petcommunity.dao;

import com.petcommunity.model.Owner;
import com.petcommunity.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OwnerDAO {

    public void addOwner(Owner owner) {
        String sql = "INSERT INTO owners (name, phone, address) VALUES (?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, owner.getName());
            stmt.setString(2, owner.getPhone());
            stmt.setString(3, owner.getAddress());
            stmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Owner> getAllOwners() {
        List<Owner> list = new ArrayList<>();
        String sql = "SELECT * FROM owners ORDER BY owner_id ASC";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                Owner owner = new Owner();
                owner.setOwnerId(rs.getInt("owner_id"));
                owner.setName(rs.getString("name"));
                owner.setPhone(rs.getString("phone"));
                owner.setAddress(rs.getString("address"));
                list.add(owner);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public Owner getOwnerById(int id) {
        Owner owner = null;
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "SELECT * FROM owners WHERE owner_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                owner = new Owner();
                owner.setOwnerId(rs.getInt("owner_id"));
                owner.setName(rs.getString("name"));
                owner.setPhone(rs.getString("phone"));
                owner.setAddress(rs.getString("address"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return owner;
    }

    public void updateOwner(Owner owner) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "UPDATE owners SET name=?, phone=?, address=? WHERE owner_id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, owner.getName());
            ps.setString(2, owner.getPhone());
            ps.setString(3, owner.getAddress());
            ps.setInt(4, owner.getOwnerId());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteOwner(int id) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "DELETE FROM owners WHERE owner_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

